//
//  TradeAlertView.m
//  BTCoin
//
//  Created by zzg on 2018/4/14.
//  Copyright © 2018年 Robin. All rights reserved.
//

#import "TradeAlertView.h"
#import "TradeAlertTableViewCell.h"
@interface TradeAlertView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView   *tableview;
@property (nonatomic, strong) UIView    *headView;      //顶部view
@end
@implementation TradeAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [self addSubview:self.tableview];
    }
    return self;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kScreenHeight / 2, kScreenWidth, kScreenHeight/2) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.bounces = YES;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableview registerNib:[UINib nibWithNibName:@"TradeAlertTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TradeAlertTableViewCell"];
    }
    return _tableview;
}

#pragma mark - tabelview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.sourceArr.count;
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TradeAlertTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TradeAlertTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TradeAlertTableViewCell" owner:nil options:nil] firstObject];
    }
//    TradeAlertViewModel *model = self.sourceArr[indexPath.row];
//    cell.titleNameLa.text = model.titleStr;
    cell.titleNameLa.text = @"USDT";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TradeAlertViewModel *model = self.sourceArr[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(setSelectedAlertListWithModel:)]) {
        [self.delegate setSelectedAlertListWithModel:model];
    }
    [self removeFromSuperview];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}

- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = [UIColor whiteColor];
        UILabel *name = [[UILabel alloc] init];
        name.textAlignment = NSTextAlignmentCenter;
        name.textColor = [UIColor blackColor];
        name.font = [UIFont systemFontOfSize:20];
        name.text = @"选择币种";
        [_headView addSubview:name];
        
        [name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(_headView);
        }];
        UIButton *deleteBtn = [[UIButton alloc] init];
        [deleteBtn addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [deleteBtn setImage:[UIImage imageNamed:@"trade_Alert_Delete"] forState:UIControlStateNormal];
        [_headView addSubview:deleteBtn];
        
        [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.mas_equalTo(_headView);
        }];
    }
    return _headView;
}

- (void)deleteButtonAction {
    [self removeFromSuperview];
}

- (void)setSourceArr:(NSMutableArray *)sourceArr {
    _sourceArr = sourceArr;
    [self.tableview reloadData];
}
@end
